package com.itheima.prince.smartpeking.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itheima.prince.smartpeking.R;
import com.itheima.prince.smartpeking.activity.MainActivity;
import com.itheima.prince.smartpeking.base.BaseFragment;
import com.itheima.prince.smartpeking.base.BasePager;
import com.itheima.prince.smartpeking.bean.NewsCenterBean;
import com.itheima.prince.smartpeking.impl.NewsPager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class LeftFragment extends BaseFragment {
    private List<NewsCenterBean.DataBean> mdata;
    private ListView listView;
    public int currentIndex = 0;
    private MyBaseAdapter myBaseAdapter;

    @Override
    public void initData() {
        listView.setSelector(android.R.color.transparent);
        myBaseAdapter = new MyBaseAdapter();
        listView.setAdapter(myBaseAdapter);
        listView.setDividerHeight(0);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //实现listview的单选效果，就是使用notifydatachange重新调用getview
                currentIndex = i;
                myBaseAdapter.notifyDataSetChanged();
                Log.d("aaa","danxuan");
                MainActivity mainActivity = (MainActivity) mactivity;
                ContentFragment rightFragment = mainActivity.getRightFragment();
                NewsPager newsPager = rightFragment.getNewsPager();
                newsPager.switchMenuPager(i);
                SlidingMenu slidingMenu = mainActivity.getSlidingMenu();
                slidingMenu.toggle();



            }
        });
    }

    @Override
    protected View initView() {
        listView = new ListView(mactivity);
        listView.setPadding(0,150,0,0);
        return listView;
    }
    public void setLeftContent(List<NewsCenterBean.DataBean> data){
       this.mdata = data;
    }

    private class MyBaseAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (mdata ==null){
                return 0;
            }
            return mdata.size();
        }

        @Override
        public Object getItem(int i) {
            return mdata.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView textview = (TextView) View.inflate(mactivity,R.layout.view_leftfragment,null);
            textview.setText(mdata.get(i).getTitle());
            if (i==currentIndex){
                textview.setEnabled(true);
            }else {
                textview.setEnabled(false);
            }

            return textview;
        }
    }
}
